package cn.tedu._04weibo.controller;


import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboAddParam;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.UserVO;
import cn.tedu._04weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._04weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/weibo")
@Tag(name = "02-微博管理接口")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("insert")
    @Operation(summary = "发布微博接口")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        log.debug("传过来的微博内容："+weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        int row=weiboMapper.insertByContent(weibo);
        return JsonResult.ok();
    }

    @GetMapping("selectIndex")
    @Operation(summary = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectIndex();
        return JsonResult.ok(weiboIndexVOList);
    }

    @GetMapping("selectById")
    @Operation(summary = "微博详情页")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(Long id){
        if(id<100 || id>500){
            throw new IllegalArgumentException("参数id不能小于100或者大于500");
        }
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }


}
